﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ManilaToday
{
    public partial class MainForm : Form
    {
        readonly SettingsManager _manager = new SettingsManager();

        public MainForm()
        {
            InitializeComponent();
            _manager.Init();

            settingsManagerBindingSource.DataSource = _manager;

            var data = _manager.GetPlugins();
            pluginList.Items.Clear();
            foreach (PluginInfo info in data.OrderBy( s => s.Order))
            {
                var item = new ListViewItem(info.Name) {Checked = info.Enabled};
                item.Tag = info;
                if (info.Type != 4)
                 item.ForeColor = Color.Gray;
                pluginList.Items.Add(item);
            }
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            List<PluginInfo> info = new List<PluginInfo>();
            
            int i = 0;
            foreach (ListViewItem item in pluginList.Items)
            {
                info.Add(new PluginInfo { Enabled  = item.Checked, Name = item.Text, Order = i++ });
            }
            _manager.Update(info);
            _manager.SaveSettings();

            IntPtr manila = Native.FindWindow("Manila", null);

            Native.PostMessage(manila, Native.WM_USER + 250, 0, 0);
        }

        private void pluginList_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool isElementSelected = pluginList.SelectedIndices.Count == 1;

            buttonUp.Enabled = isElementSelected && pluginList.SelectedIndices[0] > 0;
            buttonDown.Enabled = isElementSelected && pluginList.SelectedIndices[0] < pluginList.Items.Count - 1;
            buttonOptions.Enabled = isElementSelected && ((PluginInfo)pluginList.Items[pluginList.SelectedIndices[0]].Tag).HasOptions;
        }

        private void buttonUp_Click(object sender, EventArgs e)
        {
            if (pluginList.SelectedIndices.Count != 1)
                return;

            int selectedIndex = pluginList.SelectedIndices[0];

            ListViewItem item = pluginList.Items[selectedIndex];
            pluginList.Items.Remove(item);
            pluginList.Items.Insert(selectedIndex - 1, item);
            item.Selected = true;
        }

        private void buttonDown_Click(object sender, EventArgs e)
        {
            if (pluginList.SelectedIndices.Count != 1)
                return;

            int selectedIndex = pluginList.SelectedIndices[0];

            ListViewItem item = pluginList.Items[selectedIndex];
            pluginList.Items.Remove(item);
            pluginList.Items.Insert(selectedIndex + 1, item);
            item.Selected = true;
        }

        private void buttonOptions_Click(object sender, EventArgs e)
        {
            PluginInfo pluginInfo = ((PluginInfo) pluginList.Items[pluginList.SelectedIndices[0]].Tag);

            if (!string.IsNullOrEmpty(pluginInfo.DllPath))
            {
                Native.RunOptionsDialog(pluginInfo.DllPath, this.Handle);
                //IntPtr libHandle = Native.LoadLibrary(pluginInfo.DllPath);
                //if (libHandle != IntPtr.Zero)
                //{
                //    IntPtr windowProc = Native.GetProcAddress(libHandle, new IntPtr(241));
                //    if (windowProc != IntPtr.Zero)
                //    {
                //        IntPtr hwnd = Native.CreateDialog(libHandle, new IntPtr(500), this.Handle, windowProc);
                //        if (hwnd != IntPtr.Zero)
                //            Native.ShowWindow(hwnd, 5);
                //    }

                //    Native.FreeLibrary(libHandle);
                //}
            }

            
        }

        private void cbDefaultBackground_CheckStateChanged(object sender, EventArgs e)
        {
            tbBackgroundFileName.Enabled = !cbDefaultBackground.Checked;
            btFileBrowse.Enabled = !cbDefaultBackground.Checked;
        }

        private void btFileBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbBackgroundFileName.Text = openFileDialog1.FileName;
                _manager.BackgroundPath = openFileDialog1.FileName;
            }
        }

        private void cbOverride_CheckStateChanged(object sender, EventArgs e)
        {
            nbHeight.Enabled = cbOverride.Checked;
            nbHeightLandscape.Enabled = cbOverride.Checked;
        }

    }
}